var app = getApp();
const apiHost = require('../../../common/api_host.js');
var utils = require('../../../utils/utils.js');
var httpJson=require('../../../utils/http_json.js');
Page({
  change_choose:function(e){
    // if(e.currentTarget.dataset.myid=='0'){
      this.setData({
        choose:e.currentTarget.dataset.myid
      })
      if(e.currentTarget.dataset.myid==1){
        if(!this.data.brains){
          this.getBrasinContent(0);
        }
        
      }
      if(e.currentTarget.dataset.myid==2){
        if(!this.data.discuss){
          this.getDiscussContent(0);
        }
        
      }

  },
  toDetail:function(e){
    console.log(e)
  },
  /**
   * 页面的初始数据
   */
  data: {
    // toDetail:this.toDetail,
    rooms:[
      
     
     
    ],
    brainHoles:[
     
    ],
   
    choose:'0',roomspageNum:0,brainspageNum:0,discusspageNum:0
  },
  getuser:function(){
    var  url=`${apiHost.config.portalApiHost}/portal/user/update`;
    let data={}
    httpJson.post(url,data,res=>{
      if(res.data.errorCode==0){
        
        this.setData({
          userMsg:res.data.body
        })
      }
    })
  },
  getRoomContent:function(pageNum){
    var  url=`${apiHost.config.portalApiHost}/portal/user/roomContent`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
       
        let rooms=res.data.body.rows;
        if(rooms.length==0){
          this.setData({
            RoomNoContent:true,
           
          })
          return;
        }
          /**start1套路代码 */
            if(pageNum==0){
              this.data.rooms=res.data.body.rows
            }
            /**判断是否还有数据 */
            if(res.data.body.totalPage==this.data.roomspageNum+1){
              this.setData({
               roomshasMore:false
              })
            }
            /**此时进行分页处理 */
            if(data.pageNum>0){
              this.data.rooms= this.data.rooms.concat(res.data.body.rows);
            }
        /**end1套路代码 */
       
       
        rooms.sort((p1,p2)=>{
          p1.createdTime-p2.createdTime
        })
        rooms.forEach((ele)=>{
          ele.mytime=utils.formatDate2(ele.createdTime)
        })
       
        this.setData({
          rooms:this.data.rooms,
          roomsTotalCount:res.data.body.totalCount
        })
        
       
      }
    })
  },
  getBrasinContent:function(pageNum){

    var  url=`${apiHost.config.portalApiHost}/portal/user/brainContent`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
        let brains=res.data.body.rows;
        if(brains.length==0){
          this.setData({
            BrasinNoContent:true
          })
          return;
        }
        /**start1套路代码 */
            if(pageNum==0){
              this.data.brains=res.data.body.rows
            }
            /**判断是否还有数据 */
            if(res.data.body.totalPage==this.data.brainspageNum+1){
              this.setData({
                brainshasMore:false
              })
            }
            /**此时进行分页处理 */
            if(data.pageNum>0){
              this.data.brains= this.data.brains.concat(res.data.body.rows);
            }
        /**end1套路代码 */
       
        brains.sort((p1,p2)=>{
          p1.createdTime-p2.createdTime
        })
        brains.forEach((ele)=>{
          ele.mytime=utils.formatDate2(ele.createdTime)
        })
       
        this.setData({
          brains:this.data.brains,
          brainsTotalCount:res.data.body.totalCount
        })
         
       
      }
    })
  },
  getDiscussContent:function(pageNum){

    var  url=`${apiHost.config.portalApiHost}/portal/user/disscountContent`;
    let data={pageNum,pageSize:10}
    httpJson.get(url,data,res=>{
      if(res.data.errorCode==0){
        setTimeout(()=>{
          wx.hideLoading()
          wx.stopPullDownRefresh()
        },1000)
        let discuss=res.data.body.rows;
        if(discuss.length==0){
          this.setData({
            DiscussNoContent:true
          })
          return;
        }
        /**start1套路代码 */
            if(pageNum==0){
              this.data.discuss=res.data.body.rows
            }
            /**判断是否还有数据 */
            if(res.data.body.totalPage==this.data.discusspageNum+1){
              this.setData({
                discusshasMore:false
              })
            }
            /**此时进行分页处理 */
            if(data.pageNum>0){
              this.data.discuss= this.data.discuss.concat(res.data.body.rows);
            }
        /**end1套路代码 */
       
        discuss.sort((p1,p2)=>{
          p1.createdTime-p2.createdTime
        })
        discuss.forEach((ele)=>{
          ele.mytime=utils.formatDate2(ele.createdTime)
        })
       
        this.setData({
          discuss:this.data.discuss,
          discussTotalCount:res.data.body.totalCount
        })
         
       
      }
    })
  },
addContentLike:function(e){
   
    let contentId=app.E(e,"myid");
    let userLike=app.E(e,'islike');
    let index=app.E(e,'idx');
    let mylist=app.E(e,'mylist');
    console.log(contentId,userLike,index)
    var  url;
    if(userLike){
      utils.successToast('取消惊叹成功',500)
      url =`${apiHost.config.portalApiHost}/portal/content/removeLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          var params={};
          if(mylist=='rooms'){
            
            var str=`rooms[${index}].userLike`;
            this.data.rooms[index].likeNum--
            debugger
            this.setData({
              rooms:this.data.rooms
            })
          }
          if(mylist=='brains'){
            
            var str=`${mylist}[${index}].userLike`;
            this.data.brains[index].likeNum--
            debugger
            this.setData({
              brains:this.data.brains
            })
          }
          params[str]=false;
         this.setData(params)
        }
      })
    }
    if(!userLike){
      utils.successToast('惊叹成功',500)
      url =`${apiHost.config.portalApiHost}/portal/content/addLike`;
      let data={contentId}
      httpJson.post(url,data,res=>{
        if(res.data.errorCode==0){
          var params={};
          if(this.data.choose==0){
            var str=`rooms[${index}].userLike`;
            this.data.rooms[index].likeNum++
           
            this.setData({
              rooms:this.data.rooms
            })
          }
          if(this.data.choose==1){
            var str=`brains[${index}].userLike`;
            this.data.brains[index].likeNum++
            this.setData({
              brains:this.data.brains
            })
          }
          params[str]=true;
         
         this.setData(params)
        
        }
      })
    }
  
},
toDetail:function(){
  
},
reply:function(){
  console.log()
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getuser()
    this.getRoomContent(0);
    // this.getBrasinContent(0);


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({

      title: '正在刷新',
    })
    if(this.data.choose==0){
      this.getRoomContent(0);
      
      
    }
    if(this.data.choose==1){
      this.getBrasinContent(0);
     
      
    }
    if(this.data.choose==2){
      this.getDiscussContent(0);
    }
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

     //地上发布
     if(this.data.choose==0){

      if(this.data.rooms.length<this.data.roomsTotalCount){
         this.setData({
          roomshasMore:true
         })
       
        let pageNum=++this.data.roomspageNum
         this.getRoomContent(pageNum)
      }

  }
  //地下发布
  if(this.data.choose==1){
   

    if(this.data.brains.length<this.data.brainsTotalCount){
 
      this.setData({
        brainshasMore:true
      })
      let pageNum=++this.data.brainspageNum
      this.getBrasinContent(pageNum)
    }
    

 }
 if(this.data.choose==2){
   

  if(this.data.discuss.length<this.data.discussTotalCount){

    this.setData({
      discusshasMore:true
    })
    let pageNum=++this.data.discusspageNum
    this.getDiscussContent(pageNum)
  }
  

}

  },

  /**
   * 用户点击右上角分享
   */
   
  onShareAppMessage(res) {
    app.share()
    if (res.from === 'button') {
      var content=res.target.dataset.content
     
      if(content.length>50){
        content=content.substr(0,50)+'...'
      }
      var title=content
    }
    if(res.from === 'menu'){
      var imageUrl="/images/index/reply/reply.png";
      var title='我正在惊人院，邀请你来……'
    }
    return {
      title,
      imageUrl
      
    }
  }
})